import {
  getArticleList,
  getCategoryList,
  getTagList,
  getisRecommended,
  getArticleDel,
  getCategoryDel,
  getCategorySave,
  getTagSave,
  getTagDel,
} from '@/service';
import { IArticleList, ICategory, ITag } from '@/types';
import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';

export interface ArticleModelState {
  articleList: IArticleList[];
  categoryList: ICategory[];
  tagList: ITag[];
  // tagDetailList:ITagDetailList[]
}

export interface ArticleModelType {
  namespace: 'article';
  state: ArticleModelState;
  effects: {
    getArticleList: Effect;
    getCategoryList: Effect;
    getTagList: Effect;
    getisRecommended: Effect;
    getArticleDel: Effect;
    getCategoryDel: Effect;
    getCategorySave: Effect;
    getTagSave: Effect;
    getTagDel: Effect;
  };
  reducers: {
    save: Reducer<ArticleModelState>;
  };
}

const ArticleModel: ArticleModelType = {
  // 数据模型，用来隔离数据防止相互影响
  namespace: 'article',

  // 数据模型状态，相当于vuex的state
  state: {
    articleList: [],
    categoryList: [],
    tagList: [],
    // tagDetailList:[]
  },

  // 异步操作，相当于vuex的action
  effects: {
    // 先简单记忆，*相当于async，yield相当于await
    // 总数据
    *getArticleList({ payload }, { call, put }) {
      let result = yield call(getArticleList, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            articleList: result.data[0],
            articleLength: result.data[1],
          },
        });
      }
    },
    // Category数据
    *getCategoryList({ payload }, { call, put }) {
      let result = yield call(getCategoryList, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            categoryList: result.data,
          },
        });
      }
    },
    // Tag数据
    *getTagList({ payload }, { call, put }) {
      let result = yield call(getTagList, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            tagList: result.data,
          },
        });
      }
    },
    // 首焦
    *getisRecommended({ payload }, { call, put }) {
      let result = yield call(getisRecommended, payload);
    },

    *getArticleDel({ payload }, { call, put }) {
      let result = yield call(getArticleDel, payload);
    },
    // 分类删除
    *getCategoryDel({ payload }, { call, put }) {
      let result = yield call(getCategoryDel, payload);
    },
    // 分类删除
    *getTagDel({ payload }, { call, put }) {
      let result = yield call(getTagDel, payload);
    },
    // 分类保存
    *getCategorySave({ payload }, { call, put }) {
      let result = yield call(getCategorySave, payload);
      console.log(result);
      if (result) {
        yield put({
          type: 'save',
        });
      }
    },
    // 标签保存
    *getTagSave({ payload }, { call, put }) {
      let result = yield call(getTagSave, payload);
    },
  },

  // 同步操作，相当于vuex的mutation
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};

export default ArticleModel;
